import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../page/index'
import Login from '../page/login/';
import notFoot from '../page/404';
import NProgress from '../util/nprogress';
import store from '../store'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    redirect: '/vel/index',
    component: Home,
    meta: {},
    children: [

    ]
  },
  {
    path: '/login',
    name: '登录',
    meta: {
      title: '登录',
    },
    component: Login,
  },
  {
    path: '/404',
    name: '404',
    component: notFoot,
    meta: {
      title: '404',
    },
  },
  {
    path: '*',
    redirect: '/404',
    component: notFoot,

  }
]

const router = new VueRouter({
  // mode: 'history',
  // scrollBehavior: () => ({ y: 0 }),
  scrollBehavior(to, from, savedPosition) {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve({ x: 0, y: 0 })
      }, 500)
    })
  },
  base: process.env.BASE_URL,
  routes
})

// 前置路由守卫
router.beforeEach((to, from, neth) => {
  NProgress.start()
  let data = localStorage.getItem('xm-token')
  if (to.path != '/login' && !data) {
    console.log('qqqqqqq')
    neth({ path: '/login' })
    NProgress.done()
  } else {
    if (to.path === '/login' && data) {
      neth({ path: '/' })
    }
  }
  if (!store.getters['config/getTabsList'].length && data) store.commit('changeTab')
  neth()
})
// 后置守卫
router.afterEach((to) => {
  let arr=['/login','/404']
  if(arr.indexOf(to.path)===-1){
    store.commit('config/changedefaultActive',[to.path,to.meta.title])
  }
  document.title = 'xm-' + to.meta.title
  NProgress.done()
})
export default router
